package com.yilizhiyou.controller;

import com.yilizhiyou.common.Result;
import com.yilizhiyou.dto.UserLoginDTO;
import com.yilizhiyou.dto.UserRegisterDTO;
import com.yilizhiyou.entity.User;
import com.yilizhiyou.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户控制器
 * 
 * @author 伊犁智游开发团队
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户注册、登录、信息管理等相关接口")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "新用户注册接口")
    public Result<Map<String, Object>> register(@Validated @RequestBody UserRegisterDTO registerDTO) {
        log.info("用户注册请求: {}", registerDTO.getUsername());
        Map<String, Object> result = userService.register(registerDTO);
        
        if ((Boolean) result.get("success")) {
            return Result.success("注册成功", result);
        } else {
            return Result.error((String) result.get("message"));
        }
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口")
    public Result<Map<String, Object>> login(@Validated @RequestBody UserLoginDTO loginDTO, HttpServletRequest request) {
        log.info("用户登录请求: {}", loginDTO.getUsername());
        Map<String, Object> result = userService.login(loginDTO);
        
        if ((Boolean) result.get("success")) {
            return Result.success("登录成功", result);
        } else {
            return Result.error((String) result.get("message"));
        }
    }

    @PostMapping("/logout")
    @Operation(summary = "用户退出登录", description = "用户退出登录接口")
    public Result<Void> logout(@RequestHeader("Authorization") String token) {
        log.info("用户退出登录请求");
        boolean result = userService.logout(token);
        
        if (result) {
            return Result.success();
        } else {
            return Result.error("退出失败");
        }
    }

    @GetMapping("/info")
    @Operation(summary = "获取用户信息", description = "获取当前登录用户信息")
    public Result<User> getUserInfo(@RequestHeader("Authorization") String token) {
        // 这里应该从token中解析用户ID，简化处理
        log.info("获取用户信息请求");
        // 实际应该从JWT token中解析用户ID
        User user = userService.getById(1L); // 临时使用ID为1的用户
        if (user != null) {
            user.setPassword(null); // 清除密码信息
            return Result.success(user);
        } else {
            return Result.error("用户不存在");
        }
    }

    @PutMapping("/info")
    @Operation(summary = "更新用户信息", description = "更新用户基本信息")
    public Result<Void> updateUserInfo(@Validated @RequestBody User user) {
        log.info("更新用户信息请求: {}", user.getId());
        boolean result = userService.updateUserInfo(user);
        
        if (result) {
            return Result.success();
        } else {
            return Result.error("更新失败");
        }
    }

    @PostMapping("/change-password")
    @Operation(summary = "修改密码", description = "用户修改密码接口")
    public Result<Void> changePassword(
            @Parameter(description = "旧密码") @RequestParam String oldPassword,
            @Parameter(description = "新密码") @RequestParam String newPassword,
            @RequestHeader("Authorization") String token) {
        log.info("修改密码请求");
        // 实际应该从JWT token中解析用户ID
        boolean result = userService.changePassword(1L, oldPassword, newPassword);
        
        if (result) {
            return Result.success();
        } else {
            return Result.error("密码修改失败");
        }
    }

    @PostMapping("/reset-password")
    @Operation(summary = "重置密码", description = "通过手机号重置密码")
    public Result<Void> resetPassword(
            @Parameter(description = "手机号") @RequestParam String phone,
            @Parameter(description = "新密码") @RequestParam String newPassword,
            @Parameter(description = "短信验证码") @RequestParam String smsCode) {
        log.info("重置密码请求: {}", phone);
        boolean result = userService.resetPassword(phone, newPassword, smsCode);
        
        if (result) {
            return Result.success();
        } else {
            return Result.error("密码重置失败");
        }
    }

    @PostMapping("/send-sms")
    @Operation(summary = "发送短信验证码", description = "发送短信验证码接口")
    public Result<Void> sendSmsCode(
            @Parameter(description = "手机号") @RequestParam String phone,
            @Parameter(description = "验证码类型") @RequestParam String type) {
        log.info("发送短信验证码请求: {}, 类型: {}", phone, type);
        boolean result = userService.sendSmsCode(phone, type);
        
        if (result) {
            return Result.success();
        } else {
            return Result.error("验证码发送失败");
        }
    }

    @GetMapping("/check-username")
    @Operation(summary = "检查用户名", description = "检查用户名是否可用")
    public Result<Boolean> checkUsername(@Parameter(description = "用户名") @RequestParam String username) {
        log.info("检查用户名请求: {}", username);
        User user = userService.findByUsername(username);
        return Result.success(user == null);
    }

    @GetMapping("/check-phone")
    @Operation(summary = "检查手机号", description = "检查手机号是否已注册")
    public Result<Boolean> checkPhone(@Parameter(description = "手机号") @RequestParam String phone) {
        log.info("检查手机号请求: {}", phone);
        User user = userService.findByPhone(phone);
        return Result.success(user == null);
    }

    @GetMapping("/check-email")
    @Operation(summary = "检查邮箱", description = "检查邮箱是否已注册")
    public Result<Boolean> checkEmail(@Parameter(description = "邮箱") @RequestParam String email) {
        log.info("检查邮箱请求: {}", email);
        User user = userService.findByEmail(email);
        return Result.success(user == null);
    }
}
